package person.lance.socketdemo;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket socket = serverSocket.accept();
        System.out.println("有人来了.....");
        OutputStream out = socket.getOutputStream();
        InputStream in = socket.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//        String line = reader.readLine();
//        System.out.println(line);
//        out.write(("hello" + line + "\n").getBytes());
        //根据输入输出流和客户端连接
        InputStream inputStream=socket.getInputStream();//得到一个输入流，接收客户端传递的信息
        InputStreamReader inputStreamReader=new InputStreamReader(inputStream);//提高效率，将自己字节流转为字符流
        BufferedReader bufferedReader=new BufferedReader(inputStreamReader);//加入缓冲区
        String temp=null;
        String info="";
        while((temp=bufferedReader.readLine())!=null){
            info+=temp;
            System.out.println("已接收到客户端连接");
            System.out.println("服务端接收到客户端信息："+info+",当前客户端ip为："+socket.getInetAddress().getHostAddress());
        }

        reader.close();
        out.close();
        socket.close();
        ;

    }
}
